#intended to be for the methods shared between the duplicate state and original game

module DominoesGameFunctions
  
  def check_winner
    if @players_play
      @player.hand.empty? || @player.points >= DominoesGame::TARGET_POINTS
    else
      @opp.hand.empty? || @opp.points >= DominoesGame::TARGET_POINTS
    end
  end
  
  def adjust_score
    #need to check if winner, then add remaining points of opposing hand
    if @board.sum_ends % DominoesGame::SCORER == 0
      p = @board.sum_ends
      @players_play ? @player.add_points(p): @opp.add_points(p)
    end
  end
  
  def adjust_probability_table
    p = initial_probability(@opp.hand.size, @prob_hash.size - @prob_hash.num_sticky)
    @prob_hash.reset(p)
  end
  
  def play_domino domino, dominee
    @board.place_domino domino, dominee
    
    if (dominee.is_a? Domino) && !@board.has_spinner? && dominee.is_double? && @board.size > 2
      d1 = Domino.new dominee.head, dominee.tail, true
      d2 = Domino.new dominee.head, dominee.tail, true
      @board.new_trail(d1)
      @board.new_trail(d2)
    end
    
    @pool.remove domino
    @players_play ? @player.hand.remove(domino) : @opp.hand.remove(domino)
    @prob_hash.remove domino
    true
  end
  
  def current_player
    @players_play ? @player : @opp
  end
  
  def valid_generator(doms, board)
    if !board.game_started?
      doms.inject({}) { |memo, d| memo[d] = [DominoesGame::WILDCARD]; memo }
    else
      doms.inject({}) do |memo, d| 
        board.get_unique_ends.each do |e|
          memo[d] = ((memo[d] || []) << e) if d.to_a.include?(e.head)
        end
        memo
      end
    end
  end
  
  def print_state
    puts "\nGAME STATE:"
    puts "Board: \n#{@board}"
    puts "Pool: #{@pool.dominoes}"
    puts "Points: player #{@player.points} opponent: #{@opp.points}"
    puts "Probability Hash: #{@prob_hash}\n\n"
    
  end
  
  
end